﻿using System;
using System.Collections.Generic;
using System.Text;
using Core;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace PlugIns
{
    using System.Reflection;

    public class WPlug : _BasePlugin
    {
        private const string Name = "Выгрузка списка рейтинга в Word";
        private const string Info = "Выгрузка рейтинга списком в\nMicrosoft Office Word 2003\nАвтор: Артамонов Юрий\nВерсия: 1.0";

        public void MainMethod(SchoolClass Class,Form Owner)
        {
            bool ch = true;
            Pupil temp;
            while (ch)
            {
                ch = false;
                for (int i = 0; i < Class.group.Length-1; i++)
                {
                    if (Class.group[i].ClassPlace > Class.group[i + 1].ClassPlace)
                    {
                        ch = true;
                        temp = Class.group[i];
                        Class.group[i] = Class.group[i+1];
                        Class.group[i + 1] = temp;
                    }
                }
            }

            Word.Application app = new Word.Application();

            object template = Missing.Value;
            object newTemplate = Missing.Value;
            object documentType = Missing.Value;
            object visible = true;

            Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
            
            object start = 0;
            object end = 1;

            Word.Range point = doc.Range(ref start,ref end);
            StringBuilder builder = new StringBuilder();
            int place = 0;
            builder.AppendLine("Отчёт");
            builder.AppendLine(Class.ClassTitle + "\n");
            for (int i = 0; i < Class.group.Length; i++)
            {
                if (Class.group[i].ClassPlace != place)
                {
                    builder.AppendLine(Class.group[i].ClassPlace.ToString() + ". " +
                                          Class.group[i].Name + "  " + Class.group[i].ClassBonus.ToString());
                    place = Class.group[i].ClassPlace;
                }
                else
                {
                    builder.AppendLine("  " +
                                          Class.group[i].Name + "  " + Class.group[i].ClassBonus.ToString());
                }
            }
            builder.AppendLine("\n Максимум: " + Class.Study.Length*Class.group.Length);
            point.Text = builder.ToString();
            point.Font.Size = 14;

            app.Visible = true;
        }
        public string PlugInfo
        {
            get { return Info; }
        }
        public string PlugName
        {
            get { return Name; }
        }
    }
}
